using System;

// Plan to get the nearest enery pickup - won't return false(finish) until all energy is picked up
public class PlanGetNearestEnergy : Plan
{

	private AgentBehaviourScript agent;
	private Maze maze;
	
	private Plan currentPlan = null;	
	private	EnergyPickup energyPickup;	

	public PlanGetNearestEnergy (AgentBehaviourScript agent, Maze maze)
	{
		this.agent = agent;
		this.maze = maze;
		energyPickup = agent.getNearestEnergy();
	}
	
	public override bool execute()
	{
	
		if (energyPickup != agent.getNearestEnergy())
	    {
	    
	    	if (agent.getNearestEnergy() == null)
			{
				currentPlan = null;
				return false;
			}
	    	energyPickup = agent.getNearestEnergy();
			currentPlan = new PlanGoToObject(agent, maze, energyPickup.gameObject);
		}
		if (energyPickup == null)
		{
			currentPlan = null;
			return false; // All energy collected
		}
		
		if ( currentPlan == null || !currentPlan.execute() )
		{
			// no currentPlan or currentPlan finished
			currentPlan = new PlanGoToObject(agent, maze, energyPickup.gameObject);
		}
		
		return true;	
	}
	
	public override pathplanner getPathPlanner() {
		
		if (currentPlan	!= null) {
			return currentPlan.getPathPlanner();
		} else {
			return null;
		}
		
	}

	public override string ToString ()
	{
		string returnString = string.Format ("GetNearestEnergy( ");
		if (currentPlan != null)
			returnString += currentPlan.ToString();
		returnString += " )";
		return returnString;
	}	
	
	
}

